package com.elric.mall.controller;

import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.common.utils.ExcelFileUtil;
import com.elric.mall.dto.KzNewProjectImportDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.model.KzNewProject;
import com.elric.mall.service.KzNewProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 实现功能：
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "KzNewProjectController", description = "新上项目接口")
@RequestMapping("/newProject")
@RestController
public class KzNewProjectController extends BaseController {

    @Autowired
    KzNewProjectService kzNewProjectService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody KzNewProject kzNewProject) {
        int count = kzNewProjectService.create(kzNewProject);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody KzNewProject kzNewProject) {
        int count = kzNewProjectService.update(kzNewProject);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<KzNewProject> getItem(@PathVariable Long id) {
        KzNewProject kzNewProject = kzNewProjectService.findById(id);
        return CommonResult.success(kzNewProject);
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = kzNewProjectService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<KzNewProject>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestBody KzNewProject kzNewProject) {
        List<KzNewProject> menuList = kzNewProjectService.list(pageSize, pageNum, kzNewProject);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    /**
     * excel导入
     *
     * @param request 请求参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/importDocuments", method = RequestMethod.POST)
    @ApiOperation(value = "固定表导入", notes = "固定表导入")
    public CommonResult importDocuments(MultipartHttpServletRequest request) {
        return CommonResult.success(kzNewProjectService.importDocuments(request));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<KzNewProject> list) {
        return CommonResult.success(kzNewProjectService.insertBatch(list, type));
    }

    @ApiOperation("获取导入模板")
    @RequestMapping(value = "/getImportTemplate", method = RequestMethod.GET)
    @ResponseBody
    public void getImportTemplate(HttpServletResponse response) {
        ExcelFileUtil.toExcel(response, new ArrayList<>(), "新上项目导入模板",new KzNewProjectImportDto());
    }

}
